package com.ama.controller;

import com.ama.pojo.Company;
import com.ama.pojo.Person;
import com.ama.service.CompanyService;
import com.ama.service.PersonService;
import com.ama.utils.SpringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@Controller
public class PublishController {
    @RequestMapping("/publishInfo")
    public void publishInfo(HttpServletResponse response, HttpServletRequest request) throws IOException {
        HttpSession session = request.getSession();
        String type = request.getParameter("type");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<script>");
        Date date = new Date();
        if("个人".equals(type)){
            Person person = (Person) session.getAttribute("person");
            if(person.getPubtime()!=null){
                out.println("alert('您已发布过简历！不能重复发布！')");
            }else{
                person.setPubtime(date);
                SpringUtils.getBean(PersonService.class).publish(person);
                session.setAttribute("person", person);
                out.println("alert('发布成功！')");
            }
            out.println("window.open ('"+request.getContextPath()+"/index.jsp','_top')");
        }

        if("公司".equals(type)){
            Company company = (Company) session.getAttribute("company");
            if(company.getPubtime()!=null){
                out.println("alert('您已发布过招聘信息！不能重复发布！')");
            }else{
                company.setPubtime(date);
                SpringUtils.getBean(CompanyService.class).publish(company);
                session.setAttribute("company", company);
                out.println("alert('发布成功！')");
            }
            out.println("window.open ('"+request.getContextPath()+"/c_index.jsp','_top')");
        }
        out.println("</script>");
        out.println("</html>");
        out.flush();
        out.close();
    }
}
